
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class FactoryAdd extends JFrame{
    public FactoryAdd(String mUID){

        SQLOperation sql=new SQLOperation();

        setTitle("Equip Add");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Please complete following information.");
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);

        JPanel info=new JPanel(new GridLayout(1,1,5,5));




        JPanel typePanel=new JPanel();

        typePanel.add(new JLabel("TypeName:"));
        JTextField type=new JTextField(20);


        typePanel.add(type);
        info.add(typePanel);






        add(info,BorderLayout.CENTER);


        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FactoryDefaultFrame(mUID);
            }
        });

        JButton add=new JButton("Add");
        add.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                boolean isSame=true;
                String newTypePK="";
                String newDevicePK="";
                String[] typeNameAttri={"tName","typeID"};
                Object[][] typeData=sql.search("DeviceType",typeNameAttri,"shanchu",1);
                boolean isIdentical=false;
                String existTypePK="";
                for(int i=0;i<typeData.length;i++){
                    if(typeData[i][0].equals(type.getText())){
                        isIdentical=true;
                        existTypePK=String.valueOf(typeData[i][1]);
                        break;
                    }

                }



                if(type.getText().equals("")||type.getText().equals(null)){
                    JOptionPane.showMessageDialog
                            (null, "Please Fill in all information!", "Missing Input",
                                    JOptionPane.ERROR_MESSAGE);
                }

                else if (isIdentical){
                    String[] factoryAttri={"Manufacture_mUID"};
                    Object[][] factoryOfData= sql.search
                            ("Device",factoryAttri,"DeviceType_typeID",existTypePK);
                    Boolean isExist=false;
                    for(int i=0;i<factoryOfData.length;i++){
                        if(factoryOfData[i][0].equals(mUID)){
                            isExist=true;
                            break;
                        }
                    }
                    if(!isExist){
                        newDevicePK = "";
                        isSame = true;
                        while (isSame) {

                            isSame = false;
                            newDevicePK = "id";


                            for (int i = 1; i <= 5; i++) {
                                newDevicePK = newDevicePK + (char) ((int)(Math.random() * 10) + 48);
                            }
                            String[] pkID = {"idDevice"};
                            Object[][] pk = sql.search("Device", pkID, "shanchu", 1);
                            for (int i = 0; i < pk.length; i++) {
                                if (pk[i][0].equals(newDevicePK)) {
                                    isSame = true;
                                    break;
                                }
                            }
                        }


                        Object[] newEquipInfo =
                                {newDevicePK, 1, 0, existTypePK,mUID};

                        sql.add(newEquipInfo, "Device");


                        JOptionPane.showMessageDialog
                                (null, "Add Complete!Existed Type ID:" + existTypePK,
                                        "Success", JOptionPane.INFORMATION_MESSAGE);

                        setVisible(false);

                        new FactoryDefaultFrame(mUID);

                    }
                    else{
                        JOptionPane.showMessageDialog
                                (null, "Already Existed Type Name!", "Exist Type",
                                        JOptionPane.ERROR_MESSAGE);

                    }

                }
                else {
                    newTypePK="";
                    isSame=true;
                    while(isSame){
                        isSame=false;
                        newTypePK="ti";
                        for (int i = 1; i <= 5; i++) {
                            newTypePK = newTypePK + (char) ((int)(Math.random() * 10) + 48);
                        }
                        String[] pkID = {"typeID"};
                        Object[][] pk = sql.search("DeviceType", pkID, "shanchu", 1);
                        for (int i = 0; i < pk.length; i++) {
                            if (pk[i][0].equals(newTypePK)) {
                                isSame = true;
                                break;
                            }
                        }

                    }
                    Object[] newTypeInfo={newTypePK,type.getText(),1};

                    sql.add(newTypeInfo, "DeviceType");



                    newDevicePK = "";
                    isSame = true;
                    while (isSame) {

                        isSame = false;
                        newDevicePK = "id";


                        for (int i = 1; i <= 5; i++) {
                            newDevicePK = newDevicePK + (char) (Math.random() * 10 + 48);
                        }
                        String[] pkID = {"idDevice"};
                        Object[][] pk = sql.search("Device", pkID, "shanchu", 1);
                        for (int i = 0; i < pk.length; i++) {
                            if (pk[i][0].equals(newDevicePK)) {
                                isSame = true;
                                break;
                            }
                        }
                    }


                    Object[] newEquipInfo =
                            {newDevicePK, 1, 0, newTypePK,mUID};

                    sql.add(newEquipInfo, "Device");


                    JOptionPane.showMessageDialog
                            (null, "Add Complete!Random Type ID:" + newTypePK,
                                    "Success", JOptionPane.INFORMATION_MESSAGE);

                    setVisible(false);

                    new FactoryDefaultFrame(mUID);
                }





            }
        });




        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(add);

        add(buttons,BorderLayout.SOUTH);






        setSize(400,200);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);




    }
}
